package com.dji.common.api;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.dji.bean.BaiDuWeather;
import com.dji.bean.Compoment;
@Compoment
public class WeatherAPI {
	private final static Logger log = LoggerFactory.getLogger(WeatherAPI.class);

	public static BaiDuWeather getLoalWeather() throws IOException {
		String getURL = "http://api.map.baidu.com/telematics/v3/weather?location=北京&output=json&ak=WXQFTuoYPXx0tYGYYQ73e9PGcEuIFrj9";
		URL getUrl = new URL(getURL);
		HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
		connection.connect();
		BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
		StringBuilder responseDate = new StringBuilder();
		;
		String line = null;
		while ((line = reader.readLine()) != null) {
			responseDate.append(line);
		}
		reader.close();
		connection.disconnect();
		log.debug("百度天气API返回数据" + responseDate.toString());
		return JSON.parseObject(responseDate.toString(), BaiDuWeather.class);
	}
}
